#!/usr/bin/perl -w
#    TigerLily:  A client for the lily CMC, written in Perl.
#    Copyright (C) 1999  The TigerLily Team, <tigerlily@einstein.org>
#                                http://www.hitchhiker.org/tigerlily/
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2, as published
#  by the Free Software Foundation; see the included file COPYING.
# $Header: /home/mjr/tmp/tlilycvs/lily/tigerlily2/tools/Attic/postrelease,v 1.2 1999/04/11 22:35:48 albert Exp $

use strict;
use Config;
use TLily::Version;

use vars qw($nonono $debug %version);

$nonono = 0;
$debug  = 0;

if(! -d "CVS") {
    print STDERR "You do not appear to be a developer.  You cannot\n",
	"use this function.\n";
    exit(1);
}

%version = version_from_string($TLily::Version::VERSION);
delete $version{pre};
$version{post} = "post";
make_version_file(%version);

my $cmd = "cvs commit -m 'Automatic postcommit of version file' TLily/Version.pm";
if($nonono) {
    print "Would have executed: $cmd\n";
}
else {
    print "Committing TLily/Version.pm with:\n$cmd\n";
    system($cmd) and die "Command returned error!\n";
}

print "\n\nAfter updating the Tigerlily webpage with this new release,\n",
      "please run 'make announce' to send an announcement with version.\n";

exit(0);

sub Usage () {
    print STDERR "Usage: $0\n";
    exit(1);
}

sub do_announcement (%) {
    my(%version) = @_;
    my $version = string_from_version(%version);

    my $inp = &ask("Do you want to send out an announcement?", 'yes');
    if($inp =~ /(y|yes)/i) {
	my $ChangeLog;
	local(*CL);
	open(CL, "ChangeLog") or die "Cannot open ChangeLog for reading: $!\n";
	my $f;
	while(<CL>) {
	    if($f && ! /^Version/) { $ChangeLog .= $_; next; }
	    elsif($f && /^Version/) { last; }
	    elsif(/^Version\s+$version/) { $f = 1; $ChangeLog = $_; }
	}
	close(CL);

	my $msg = qq(From: "The Tigerlily Developers" <tigerlily\@hitchhiker.org>
To: "Tigerlily Announcement List" <tigerlily-announce\@einstein.org>
Subject: Tigerlily $version is now available

Hi All -

Tigerlily version $version is now available.

Changes in this release, from the ChangeLog:
$ChangeLog
Once Josh moves it into place, it will be available from:
http://www.hitchhiker.org/tigerlily/

Until then, you can get it from:
http://www.acm.rpi.edu/~neild/
-- 
Share and Enjoy.

The Tigerlily Developers
Damien, Albert, Josh, Jon, Steve & Matt.
----------------------------------------------------------------------------
To unsubscribe from this list, send email to <majordomo\@einstein.org> with
the words "unsubscribe tigerlily-announce" in the body.
);
	my $abort_flag;
	my $editor = $ENV{EDITOR} || $ENV{VISUAL} || "vi";
	while(1) {
	    local(*MSG);
	    print $msg;
	    my $ans = &ask("(s)end, (e)dit, or (a)bort:", "s");
	    last if $ans eq 's';
	    if($ans eq 'a') { $abort_flag = 1; last; }
	    if($ans eq 'e') {
		open(MSG, ">msgtmp.$$")
		    or die "Cannot open msgtmp.$$ for writing: $!\n";
                print MSG $msg;
                close(MSG);
		system($editor, "msgtmp.$$");
	    }
	    open(MSG, "msgtmp.$$")
		or die "Cannot open msgtmp.$$ for reading: $!\n";
	    $msg = join("", <MSG>);
	    close(MSG);
	    unlink("msgtmp.$$") or warn "Cannot unlink msgtmp.$$: $!\n";
	}
	if(!$abort_flag) {
            if($nonono) {
		print "Would have sent the message:\n$msg";
                return;
            }
            local(*SM);
            open(SM, "|/usr/sbin/sendmail -t")
		or die "Couldn't open sendmail: $!";
	    print SM $msg;
            close(SM);
	}
    }
    else {
	print "Don't forget to send an announcement of the new release\n",
	    "to tigerlily-announce\@einstein.org.\n";
    }
}

sub cvstag_from_version ($%) {
    my($s, %v) = @_;

    my $tag = "$s-$v{major}-$v{minor}";
    if($v{sub})  { $tag .= "-$v{sub}"; }
    if($v{pre})  { $tag .= "-pre";     }
    if($v{post}) { $tag .= "-post";    }
    $tag;
}

sub version_from_string ($) {
    my($v) = @_;
    my(%v);

    $v =~ /(\d+)\.(\d+)\.?(\d*)/;

    $v{major} = $1;
    $v{minor} = $2;
    $v{sub}   = $3;

    $v =~ /\d+\.\d+(?:\.\d+)?-(pre|post)/;

    if($1) { $v{$1} = $1; }

    %v;
}

sub next_version (%) {
    my(%v) = @_;
    delete $v{pre};
    delete $v{post};
    $v{sub}++;
    %v;
}

sub string_from_version (%) {
    my(%v) = @_;

    $v{major} . '.' . $v{minor} .
        ($v{sub}  ? '.' . $v{sub} : "") .
        ($v{pre}  ? '-pre'  : "") .
        ($v{post} ? '-post' : "");
}

sub check_version ($) {
    my($s) = @_;
    my(%v) = version_from_string($s);
    if($v{major} =~ /^\d+$/ && $v{minor} =~ /^\d+$/) {
       return 1;
    }
}

sub make_version_file (%) {
    my(%v) = @_;

    if($nonono) {
        print "Would have made TLily/Version.pm with version number ",
            string_from_version(%v), "\n";
        return;
    }

    print "Writing TLily/Version.pm, version ", string_from_version(%v), "\n";

    -f "TLily/Version.pm" and unlink "TLily/Version.pm"
        or die "Cannot unlink TLily/Version.pm: $!\n";

    local(*VERFILE);
    my $ver = string_from_version(%v);
    open(VERFILE, ">TLily/Version.pm")
        or die "Cannot open TLily/Version.pm for writing: $!\n";

    print VERFILE qq(#    TigerLily:  A client for the lily CMC, written in Perl.
#    Copyright (C) 1999  The TigerLily Team, <tigerlily\@einstein.org>
#                                http://www.hitchhiker.org/tigerlily/
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2, as published
#  by the Free Software Foundation; see the included file COPYING.
#
# Version.pm - Automatically generated by tools/prerelease
package TLily::Version;
\$VERSION = '$ver';
1;\n);
    close VERFILE;
    chmod(0444, "TLily/Version.pm")or die "Cannot chmod TLily/Version.pm: $!\n";
}

sub ask {
    $|=1;
    my($q,$d) = @_;
    if(length($q. " [". $d. "]: ") > 80) {
        print $q,"\n[", $d, "]: ";
    }
    else {
        print $q, " [", $d, "]: ";
    }
    my $l = <STDIN>;
    chomp $l;
    $l eq '' ? $d : $l;
}
